From 1211aeba0b1ca75ac1d6d5be0e8234541d52ae63 Mon Sep 17 00:00:00 2001 From: Factiven Date: Mon, 24 Apr 2023 14:48:02 +0700 Subject: initial commit on pre-dev --- pages/anime/[...id].js | 925 ++++++++++++++++++++++--------------------------- 1 file changed, 408 insertions(+), 517 deletions(-) (limited to 'pages/anime/[...id].js') diff --git a/pages/anime/[...id].js b/pages/anime/[...id].js index f58673d..cea025c 100644 --- a/pages/anime/[...id].js +++ b/pages/anime/[...id].js @@ -1,280 +1,359 @@ -import React, { useEffect, useState } from "react"; -import { META } from "@consumet/extensions"; +import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; +import "react-loading-skeleton/dist/skeleton.css"; -import Link from "next/link"; -import Layout from "../../components/layout"; import Head from "next/head"; - -import { closestMatch } from "closest-match"; +import Image from "next/image"; +import { useRouter } from "next/router"; +import { useEffect, useState } from "react"; +import Layout from "../../components/layout"; +import Link from "next/link"; import Content from "../../components/hero/content"; -import { getServerSession } from "next-auth/next"; -import { authOptions } from "../api/auth/[...nextauth]"; -import Image from "next/image"; +import { useSession } from "next-auth/react"; -export default function Himitsu({ - info, - color, - episodeList, - episode1, - sessions, - progress, - status, - lastPlayed, - stall, -}) { - const [showText, setShowtext] = useState(false); - const [load, setLoad] = useState(true); - const [showAll, setShowAll] = useState(false); - const [time, setTime] = useState(0); +const query = ` + query ($username: String, $status: MediaListStatus) { + MediaListCollection(userName: $username, type: ANIME, status: $status, sort: SCORE_DESC) { + user { + id + name + about (asHtml: true) + createdAt + avatar { + large + } + statistics { + anime { + count + episodesWatched + meanScore + minutesWatched + } + } + bannerImage + mediaListOptions { + animeList { + sectionOrder + } + } + } + lists { + status + name + entries { + id + mediaId + status + progress + score + media { + id + status + title { + english + romaji + } + episodes + coverImage { + large + } + } + } + } + } + } + `; + +export default function Info() { + const { data: session, status } = useSession(); + const [data, setData] = useState(null); + const [episode, setEpisode] = useState(null); + const [loading, setLoading] = useState(false); + const [progress, setProgress] = useState(null); + const [statuses, setStatuses] = useState(null); + const [stall, setStall] = useState(false); + const [color, setColor] = useState(null); - const episode = episodeList; - const epi1 = episode1; + const [showAll, setShowAll] = useState(false); - const maxItems = 3; + const [time, setTime] = useState(0); + const { id } = useRouter().query; - const nextAir = info.nextAiringEpisode; - // console.log(time); + // console.log(stall); useEffect(() => { - if (nextAir) { - setTime(convertSecondsToTime(nextAir.timeUntilAiring)); - } - - function getBrightness(color) { - const rgb = color.match(/\d+/g); - return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000; - } + const defaultState = { + data: null, + episode: null, + loading: true, + statuses: null, + progress: null, + }; - // set the text color based on the background color - function setTextColor(element) { - const backgroundColor = getComputedStyle(element).backgroundColor; - const brightness = getBrightness(backgroundColor); - if (brightness < 128) { - element.style.color = "#fff"; // white + // Reset all state variables to their default values + Object.keys(defaultState).forEach((key) => { + const value = defaultState[key]; + if (Array.isArray(value)) { + value.length + ? eval( + `set${ + key.charAt(0).toUpperCase() + key.slice(1) + }(${JSON.stringify(value)})` + ) + : eval(`set${key.charAt(0).toUpperCase() + key.slice(1)}([])`); } else { - element.style.color = "#000"; // black + eval( + `set${key.charAt(0).toUpperCase() + key.slice(1)}(${JSON.stringify( + value + )})` + ); } - } - - const elements = document.querySelectorAll(".dynamic-text"); - elements.forEach((element) => { - setTextColor(element); }); + async function fetchData() { + if (id) { + setLoading(false); + try { + const res = await fetch( + `https://api.moopa.my.id/meta/anilist/info/${id?.[0]}` + ); + const data = await res.json(); + if (data.episodes.length === 0) { + const res = await fetch( + `https://api.consumet.org/meta/anilist/info/${id[0]}?provider=9anime` + ); + const datas = await res.json(); + setColor({ backgroundColor: `${data?.color || "white"}` }); + setStall(true); + setEpisode(datas.episodes); + } else { + setEpisode(data.episodes); + } + + setColor({ backgroundColor: `${data?.color || "white"}` }); + + if (session?.user?.name) { + const response = await fetch("https://graphql.anilist.co/", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query: query, + variables: { + username: session?.user?.name, + }, + }), + }); + + const dat = await response.json(); + + const prog = dat.data.MediaListCollection; + + const gat = prog.lists.map((item) => item.entries); + const git = gat.map((item) => + item.find((item) => item.media.id === parseInt(data?.id)) + ); + const gut = git?.find( + (item) => item?.media.id === parseInt(data?.id) + ); + + if (gut) { + setProgress(gut?.progress); + if (gut.status === "CURRENT") { + setStatuses("Watching"); + } else if (gut.status === "PLANNING") { + setStatuses("Planned to watch"); + } else if (gut.status === "COMPLETED") { + setStatuses("Completed"); + } else if (gut.status === "DROPPED") { + setStatuses("Dropped"); + } else if (gut.status === "PAUSED") { + setStatuses("Paused"); + } else if (gut.status === "REPEATING") { + setStatuses("Rewatching"); + } + } + } + + if (data.nextAiringEpisode) { + setTime( + convertSecondsToTime(data.nextAiringEpisode.timeUntilAiring) + ); + } - setLoad(false); - }, [color, sessions, info.id]); + function getBrightness(color) { + const rgb = color.match(/\d+/g); + return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000; + } + // set the text color based on the background color + function setTextColor(element) { + const backgroundColor = getComputedStyle(element).backgroundColor; + const brightness = getBrightness(backgroundColor); + if (brightness < 128) { + element.style.color = "#fff"; // white + } else { + element.style.color = "#000"; // black + } + } + + const elements = document.querySelectorAll(".dynamic-text"); + elements.forEach((element) => { + setTextColor(element); + }); + + setData(data); + setLoading(true); + } catch (error) { + setTimeout(() => { + window.location.reload(); + }, 1000); + } + } + // setLoading(true); + } + fetchData(); + }, [id, session?.user?.name]); + // console.log(episode); return ( <> - {info.title?.english || info.title.romaji} - - - + {data?.title?.romaji || data?.title?.english} - - -
-
-
- -
- {info ? ( -
-
-
-
-
- {info.image && ( - <> -
- image -
- + + +
+
+
+ {data && ( + banner anime + )} +
+
+
+
+ {loading ? ( + data && ( + <> +
+ poster anime + + ) + ) : ( + + )} +
+
+
+

+ {loading ? ( + data?.title?.romaji || data?.title?.english + ) : ( + )} -

- - {/* MOBILE */} -
-

- {info.title.romaji || info.title.english} -

-
-
-

Rate:

-

{info.rating}%

-
- -
-

Format:

-

{info.type}

-
- -
-

Status:

-

{info.status}

-
- - {/* {nextAir && ( -
-

Ep {nextAir.episode}:

-

{time}

-
- )} */} -
-
- {epi1 && epi1[0] ? ( - -

- - - {" "} - WATCH -

- - ) : ( -

- + {loading ? ( + data && ( +
+
+
- - {" "} - WATCH -

- )} -
-
-
- - {/* PC */} -
-
-

- {info.title?.english || - info.title.romaji || - info.title.native} -

-
-
- {episode && episode.length} Episodes -
-
- {info.releaseDate} -
-
- {info.rating}% -
-
- {info.type} -
-
- {info.status} -
-
- Sub | EN -
- {nextAir && ( -
- Ep {nextAir.episode}: {time} + {data?.totalEpisodes} Episodes +
+
+ {data?.releaseDate} +
+
+ {data?.rating}% +
+
+ {data?.type} +
+
+ {data?.status} +
+
+ Sub | EN +
+ {data && data.nextAiringEpisode && ( +
+ Ep {data.nextAiringEpisode.episode}: {time} +
+ )}
- )} -
-
-
-

-

-
-

- -

+
+ ) + ) : ( + + )}
+ {loading ? ( +

+ ) : ( + + )} + {/*

{data.description}

*/}
+
-
-
+
+
+ {data && (
Relations
- {info.relations.length > maxItems && ( -
setShowAll(!showAll)} - > - {showAll ? "show less" : "show more"} -
- )} -
-
- {info.relations && - info.relations - .slice(0, showAll ? info.relations.length : maxItems) + )} + {data?.relations?.length > 3 && ( +
setShowAll(!showAll)} + > + {showAll ? "show less" : "show more"} +
+ )} +
+
+ {loading + ? data?.relations && + data?.relations + .slice(0, showAll ? data?.relations.length : 3) .map((relation, index) => { return (
- {relation.id}
@@ -323,287 +404,97 @@ export default function Himitsu({
); - })} -
+ }) + : [1, 2, 3].map((item) => ( +
+ +
+ ))}
- -
-
+
+
+
+ {data && (

Episodes

-
- -
- -
- {status && ( - <> -
- {status} - - status - -
- - )} -
-
- {load ? ( -

Loading...

- ) : episode ? ( - episode.map((episode, index) => { - return ( -
- -

Episode {episode.number}

- {episode.title && ( -

- "{episode.title}" -

- )} - -
-
- ); - }) - ) : ( -

No Episodes Available

- )} -
+ )} + {statuses && ( + <> +
+ {statuses} + + status + +
+ + )}
+ {loading ? ( + data && ( +
+ {episode ? ( + episode.map((episode, index) => { + return ( +
+ +

Episode {episode.number}

+ {episode.title && ( +

+ "{episode.title}" +

+ )} + +
+
+ ); + }) + ) : ( +

No Episodes Available

+ )} +
+ ) + ) : ( + <> + )}
+
+ {data && (
-
- ) : ( -
-

- 404 -

-

{`> Woops.. I think we don't have that Anime :(`}

- - Return to search - -
- )} -
- + )} +
+
+
+ + ); } -export async function getServerSideProps(context) { - context.res.setHeader( - "Cache-Control", - "public, s-maxage=10, stale-while-revalidate=59" - ); - const session = await getServerSession(context.req, context.res, authOptions); - - const { id } = context.query; - if (!id) { - return { - notFound: true, - }; - } - - const provider = new META.Anilist(); - - const [info, episodes] = await Promise.all([ - fetch(`https://api.moopa.my.id/meta/anilist/info/${id[0]}`).then((res) => - res.json() - ), - provider.fetchEpisodesListById(id[0]), - ]); - - if (!info) { - return { - notFound: true, - }; - } - - let episodeList = episodes; - let stall = false; - - if (episodes.length === 0) { - const res = await fetch( - `https://api.consumet.org/meta/anilist/info/${id[0]}?provider=9anime` - ); - const data = await res.json(); - episodeList = data.episodes; - stall = true; - } - - let progress = null; - let status = null; - let lastPlayed = null; - - if (session) { - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: ` - query ($username: String, $status: MediaListStatus) { - MediaListCollection(userName: $username, type: ANIME, status: $status, sort: SCORE_DESC) { - user { - id - name - about (asHtml: true) - createdAt - avatar { - large - } - statistics { - anime { - count - episodesWatched - meanScore - minutesWatched - } - } - bannerImage - mediaListOptions { - animeList { - sectionOrder - } - } - } - lists { - status - name - entries { - id - mediaId - status - progress - score - media { - id - status - title { - english - romaji - } - episodes - coverImage { - large - } - } - } - } - } - } - `, - variables: { - username: session?.user.name, - }, - }), - }); - - const dat = await response.json(); - - // const resp = await fetch(`/api/get-user?userName=${session?.user.name}`); - // const data = await resp.json(); - - lastPlayed = session?.user?.recentWatch?.filter( - (item) => item.title.romaji === info.title.romaji - )[0]?.episode; - - const prog = dat.data.MediaListCollection; - - const gat = prog.lists.map((item) => item.entries); - const git = gat.map((item) => - item.find((item) => item.media.id === parseInt(info.id)) - ); - const gut = git?.find((item) => item?.media.id === parseInt(info.id)); - - if (gut) { - progress = gut?.progress; - if (gut.status === "CURRENT") { - status = "Watching"; - } else if (gut.status === "PLANNING") { - status = "Planned to watch"; - } else if (gut.status === "COMPLETED") { - status = "Completed"; - } else if (gut.status === "DROPPED") { - status = "Dropped"; - } else if (gut.status === "PAUSED") { - status = "Paused"; - } else if (gut.status === "REPEATING") { - status = "Rewatching"; - } - } - } - - const color = { backgroundColor: `${info.color}` }; - const epi1 = episodes.filter((epi) => epi.number === 1); - const title = info.title?.userPreferred || "No Title"; - - return { - props: { - info: { - ...info, - title: { - ...info.title, - userPreferred: title, - }, - }, - color, - episodeList, - episode1: epi1, - sessions: session, - progress: progress || null, - status: status, - lastPlayed: lastPlayed || null, - stall, - }, - }; -} - function convertSecondsToTime(sec) { let days = Math.floor(sec / (3600 * 24)); let hours = Math.floor((sec % (3600 * 24)) / 3600); -- cgit v1.2.3